﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;

namespace PowerLife
{
    class Chunk
    {
        public int chunkSize { get; set; }//number of nodes in the chunk in one direction

        public int Xloc {get; set;}  //posistion of top left corner of chunk in relation to the world (X)
        public int Yloc { get; set; }  //posistion of top left corner of chunk in relation to the world (Y)
        public bool loaded { get; set; }
        private int worldWidth;
        private int worldHeight;

        private Node[,] Nodes_Array; //2d array of the nodes in the chunk


        
    

        /// <summary>
        /// Makes a "chunk"
        /// </summary>
        /// <param name="width">Size of the chunk</param>
        /// <param name="xloc">X posistion of top left corner of chunk in relation to the world</param>
        /// <param name="yloc">Y posistion of top left corner of chunk in relation to the world</param>
        public Chunk(int size,  int xloc, int yloc, int worldWidth, int worldHeight)
        {
            chunkSize = size;
            Xloc = xloc;
            Yloc = yloc;
            this.worldWidth = worldWidth;
            this.worldHeight = worldHeight;
            loaded = false;
        }


        /// <summary>
        /// Method to get access of the size of Array
        /// </summary>
        /// <returns>Width size of Array</returns>
        public int GetGridSize()
        {
            return chunkSize;
        }


        /// <summary>
        /// Method to get access of Node through Grid
        /// </summary>
        /// <param name="x">X value in Array</param>
        /// <param name="y">Y value in Array</param>
        /// <returns>A Node Object</returns>
        public Node GetNodeAtXY(int x, int y)
        {
            return Nodes_Array[x, y];
        }

        /// <summary>
        /// load chunk based off of seed and file data (no seed/file right now; generates a simple flat landscape at certain y)
        /// </summary>

        /* Notes on planned loading idea:
         * when loading chunk, find associated data in world file, load data. Handle exception if fails.
         * Generate world file in separate function beforehand.
         * dynamic chunk creation not straigtforward at all, Initial design will store all nodes in external file; not dynamically create based on seed.
         * 
         * At the moment, chunk simply populated by algorithm: if node position lower than 30y, it's dirt.
         */
        public void loadChunk(string worldName)
        {
            System.Diagnostics.Debug.WriteLine("Loading chunk");
            Nodes_Array = null;
            Nodes_Array = new Node[chunkSize, chunkSize];
            System.Diagnostics.Debug.WriteLine("Chunksize: " + Nodes_Array.Length.ToString());
            if (Nodes_Array == null)
            {
                System.Diagnostics.Debug.WriteLine("Error, did not allocame memory for chunk");
            }

            if (Xloc < 0 || Yloc < 0 || Xloc > worldWidth-1 || Yloc > worldHeight-1)
            {
                for (int x = 0; x < chunkSize; x++)
                {
                    for (int y = 0; y < chunkSize; y++)
                    {
                        Nodes_Array[x, y] = new Node(0);
                    }

                }
            }
            else
            {
                if (Nodes_Array.Length != 10000)
                {
                    System.Diagnostics.Debug.WriteLine("SOMETHING WENT WROOONG");
                }

                string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                using (StreamReader sr = new StreamReader(mydocpath + "\\" + worldName + ".world"))
                {
                    int xChunks = Convert.ToInt32(sr.ReadLine());
                    int yChunks = Convert.ToInt32(sr.ReadLine());
                    int ChunkSize = Convert.ToInt32(sr.ReadLine());
                    //x loc is always x columns away from left
                    //y loc is always y+3  lines down file
                    for (int y = 0; y < Yloc + 1; y++)
                    {
                        sr.ReadLine();
                    }

                    {
                        if (Nodes_Array.Length != 10000)
                        {
                            System.Diagnostics.Debug.WriteLine("SOMETHING WENT WROOONG");
                        }
                        char[] trashbuff = new char[1000000];
                        char[] nodeVal = new char[1];
                        if (Nodes_Array.Length != 10000)
                        {
                            System.Diagnostics.Debug.WriteLine("SOMETHING WENT WROOONG");
                        }
                        if (Xloc != 0)
                            sr.ReadBlock(trashbuff, 0, Xloc - 1);
                        for (int y = 0; y < ChunkSize; y++)
                        {
                            for (int x = 0; x < ChunkSize; x++)
                            {
                                sr.ReadBlock(nodeVal, 0, 1);

                                //System.Diagnostics.Debug.WriteLine("loading value: " + (Convert.ToInt32(nodeVal[0]) - 48));
                                if (Nodes_Array.Length != 10000)
                                {
                                    System.Diagnostics.Debug.WriteLine("SOMETHING WENT WROOONG");
                                }
                                else
                                {
                                    Nodes_Array[x, y] = new Node(Convert.ToInt32(nodeVal[0]) - 48);
                                }
                            }

                            sr.ReadLine();
                            if (Xloc != 0)
                                sr.ReadBlock(trashbuff, 0, Xloc - 1);

                        }
                    }

                }
            }
            loaded = true;
                
        }
        

        public void freeChunk()
        {
            Nodes_Array = null;
        }


    }
}
